﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using IronOne.SalesOne.DataObjects;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/index.aspx");
        }

        /// <Author>Suren Manawatta</Author>
        /// <Date>2012-10-30</Date>
        /// <summary>
        /// If login success with Ceylonco database
        /// SalesOne system will automatically logged in the user in SalesOne database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool loginSuccess = false;
                string userName = UserName.Text.Trim().ToLower();
                string password = Password.Text;
                
                // Auth with Ceylinco db
                LinqUser linqU = new LinqUser();
                loginSuccess = linqU.GetAuthUserOverCeylincoDb(userName, password);

                if (loginSuccess)
                {
                    // Use the mapping table to get the SalesOne username
                    string mappedUsername = linqU.GetUserMappingDetails(userName);

                    if (mappedUsername == null)
                    {
                        FailureText.Text = "Your account is not linked with the SalesOne database.";
                    }
                    else
                    {
                        if (Membership.GetUser(mappedUsername).IsApproved == false ||
                            Membership.GetUser(mappedUsername).IsLockedOut)
                        {
                            FailureText.Text = "Your account is disabled. Please contact the administrator.";
                        }
                        else
                        {
                            FormsAuthentication.RedirectFromLoginPage(mappedUsername, true);
                            Response.Redirect("~/index.aspx");
                        }
                    }
                }
                else
                {
                    FailureText.Text = "Your login attempt was not successful. Please try again.";
                    Password.Focus();
                }
            }
            catch (Exception ee)
            {
                StreamWriter file = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ZLog.txt", true);
                file.WriteLine(ee.Message);
                file.WriteLine(ee.InnerException);
                file.Close();
                FailureText.Text = ee.Message + " - " + ee.InnerException + " Internal error occurred. Please try again.";
            }
        }

        // Commented By Suren Manawatta on 2012-10-30
        //protected void Login1_LoginError(object sender, EventArgs e)
        //{
        //    System.Web.UI.WebControls.Login log = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
        //    TextBox txt = (TextBox)log.FindControl("UserName");

        //    if (Membership.GetUser(txt.Text) != null)
        //    {
        //        if (Membership.GetUser(txt.Text).IsApproved == false || Membership.GetUser(txt.Text).IsLockedOut == true)
        //        {
        //            log.FailureText = "Your account is disabled. Please contact administrator";
        //        }  
        //    }
        //}
    }
}
